package guo.mybatis.statement;

import guo.mybatis.binding.MapperMethod;

import guo.mybatis.config.Configuration;
import guo.mybatis.result.SimpleResultSetHandler;
import guo.mybatis.result.ResultSetHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class StatementHandler {
	private Configuration configuration;

	private ResultSetHandler resultSetHandler;

	public StatementHandler(Configuration configuration) {
		this.configuration = configuration;
		this.resultSetHandler=new SimpleResultSetHandler();
	}
	public <T> T query(MapperMethod method, Object parameter) throws Exception {

		Connection connection= configuration.openConn();
		PreparedStatement preparedStatement = connection.prepareStatement(String.format(method.getSql(), Integer.parseInt(String
				.valueOf(parameter))));
		preparedStatement.execute();
		return  resultSetHandler.handle(preparedStatement,method);
	}
}
